import { getShowString, LineHeight, ShowStringData } from "../utils/mirUtil";
import { loopMongen } from "../全局对象/FreshMon";
import { getItemMangae } from "../系统功能/GlobalObjectManage";
import { ActorCustomKeyList, CampEnum, GmSkill, HumanCustomSkill, HumanSkillList } from "../配置/基础配置";
import { StrengSkillList } from "../配置/强化技能";
import { CheckType, ItemCustomSet } from "../配置/鉴定系统/鉴定系统";
import { changeCustomAbility, checkItems } from "../附加功能/鉴定";

export function Main(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    let GM回血 = Player.V.GM回血
    let 伤害提示 = Player.V.伤害提示
    let showNameList = [
        {
            name: '领取金币',
            fucName: '测试.领取金币'
        },
        {
            name: '稻草人',
            fucName: '测试.callMob(稻草人,10)'
        },
        {
            name: '精英祖玛教主',
            fucName: '测试.callMob(精英祖玛教主,1,1)'
        },
        {
            name: '宠物半兽人',
            fucName: '测试.callMob(宠物半兽人,1,1)'
        },
        {
            name: '刷怪',
            fucName: '测试.怪物'
        },
        {
            name: '领取技能',
            fucName: '测试.领取技能'
        },
        {
            name: '领取技能无强化',
            fucName: '测试.领取技能无强化'
        },
        {
            name: '清理技能',
            fucName: '测试.清理技能'
        },
        {
            name: '爆率',
            fucName: '测试.爆率'
        },
        {
            name: '运9',
            fucName: '测试.运9'
        },
        {
            name: '装备',
            fucName: '测试.查看领取装备'
        },
        {
            name: '装备无属性',
            fucName: '测试.查看领取装备(2)'
        },
        {
            name: '技能神石',
            fucName: '测试.领取物品(技能神石,10)'
        },
        {
            name: `测试回血(${GM回血 ? '已开启' : '已关闭'})`,
            fucName: '测试.测试回血'
        },
        {
            name: `伤害提示(${伤害提示 ? '已开启' : '已关闭'})`,
            fucName: '测试.伤害提示'
        },
        {
            name: `测试鉴定属性`,
            fucName: '测试.测试鉴定属性列表'
        },
        {
            name: `测试鉴定属性操作`,
            fucName: '测试.测试鉴定属性操作'
        },
        {
            name: `等级`,
            fucName: '测试.等级'
        },
        {
            name: `扣血`,
            fucName: '测试.扣血'
        },
        {
            name: `防御100`,
            fucName: '测试.改变防御(100)'
        },
        {
            name: `防御180`,
            fucName: '测试.改变防御(180)'
        },
        {
            name: `防御0`,
            fucName: '测试.改变防御(0)'
        },
        {
            name: `致命一击`,
            fucName: '测试.致命一击'
        },
        {
            name: `开始伤害统计`,
            fucName: '测试.开始伤害统计'
        },
        {
            name: `打印伤害统计`,
            fucName: '测试.打印伤害统计'
        },
        {
            name: `结束伤害统计`,
            fucName: '测试.结束伤害统计'
        },
        {
            name: `新技能`,
            fucName: '测试.新技能'
        },
        {
            name: `怪物数量`,
            fucName: '测试.怪物数量'
        },
    ]

    let ItemsString = getShowString(showNameList, 0, 0, 100);

    Npc.Say(Player, ItemsString)

    // let ItemsString = `<{S=领取金币;C=251;X=60;Y=30}/@测试.领取金币>`;
    // ItemsString += `<{S=稻草人;C=251;X=10;Y=0}/@测试.callMob(稻草人,10)>`;
    // ItemsString += `<{S=精英祖玛教主;C=251;X=80;Y=0}/@测试.callMob(精英祖玛教主,1,1)>`;
    // ItemsString += `<{S=刷怪;C=251;X=180;Y=0}/@测试.怪物>`;
    // ItemsString += `<{S=领取技能;C=251;X=10;Y=30}/@测试.领取技能>`;
    // ItemsString += `<{S=爆率;C=251;X=10;Y=60}/@测试.爆率>`;
    // Npc.Say(Player, ItemsString)
}

export function callMob(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    let name = Args.Str[0];
    let num = Args.Int[1] || 1;
    let flag = Args.Int[2]
    loopMongen(Player.GetMap().GetMapID(), name, Player.MapX, Player.MapY, 5, num, (list: TActorList) => {
        if (flag) {
            // for(let i=0;i<list.Count;i++)
            // {
            //     let item=list.Actor(i);
            //     item.TriggerAINpcExecute = true;
            //     item.TriggerDieEvent=true;
            //     item.AttackSpeed=1000;
            // }
        }
    })


}

export function 领取金币(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    if (Player.CanAddGameMoney(1, 100000000)) {
        Player.AddGameMoney(1, 100000000, '测试')
    }
    if (Player.CanAddGameMoney(2, 100000)) {
        Player.AddGameMoney(2, 100000, '测试')
    }
}

export function 领取技能(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {

    Player.ClearSkill()



    let skillList = HumanSkillList[Player.Job];
    if (skillList && skillList.length > 0) {
        for (let i = 0; i < skillList.length; i++) {
            Player.AddSkill(skillList[i], 3)
        }
    }
    skillList = HumanCustomSkill;
    if (skillList && skillList.length > 0) {
        for (let i = 0; i < skillList.length; i++) {
            Player.AddSkill(skillList[i], 0)
        }
    }

    if (Player.IsAdmin) {
        skillList = GmSkill;
        if (skillList && skillList.length > 0) {
            for (let i = 0; i < skillList.length; i++) {
                Player.AddSkill(skillList[i], 0)
            }
        }
    }

    let strengSkillList = StrengSkillList[Player.Job];
    if (strengSkillList && strengSkillList.length > 0) {
        for (let i = 0; i < strengSkillList.length; i++) {
            let skill: TUserMagic = Player.FindSkill(strengSkillList[i].skillName)
            if (skill) {
                skill.Strengthen = 10
            }
        }
    }
}
export function 领取技能无强化(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {

    Player.ClearSkill()

    let skillList = HumanSkillList[Player.Job];
    if (skillList && skillList.length > 0) {
        for (let i = 0; i < skillList.length; i++) {
            Player.AddSkill(skillList[i], 3)
        }
    }

    skillList = HumanCustomSkill;
    if (skillList && skillList.length > 0) {
        for (let i = 0; i < skillList.length; i++) {
            Player.AddSkill(skillList[i], 0)
        }
    }

    if (Player.IsAdmin) {
        skillList = GmSkill;
        if (skillList && skillList.length > 0) {
            for (let i = 0; i < skillList.length; i++) {
                Player.AddSkill(skillList[i], 0)
            }
        }
    }
}
export function 清理技能(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {

    Player.ClearSkill()

}

export function 爆率(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    Player.AddedAbility.SetItemRate(1000000)
    Player.RecalcAbilitys()
}

export function 运9(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    Player.AddedAbility.SetLuck(10)
    Player.RecalcAbilitys()
}

export function 改变防御(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    let num = Args.Int[0]
    Player.AddedAbility.SetAC(num)
    Player.AddedAbility.SetACMax(num)
    Player.AddedAbility.SetMAC(num)
    Player.AddedAbility.SetMACMax(num)
    Player.RecalcAbilitys()
}
export function 致命一击(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    Player.AddedAbility.SetPunchHit(100)
    Player.AddedAbility.SetPunchHitAppendDamage(100)
    Player.RecalcAbilitys()
}
export function 怪物(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    const S =
        `
    <请输入怪物名称/@@测试.InputString1(请输入怪物名称)>\\
    <离 开/@exit>`
    //InputString1中使用了3个参数这个,第一个为"请输入：",这个固定为客户端显示的标题,第二三个则是在调用InputString1时可访问到的参数

    //InputString和InputInteger一样,如果一旦使用了参数,则第一个参数就是标题,有效参数从第二个开始,玩家输入的内容则在返回的参数中的最后一个

    //比如这里,将会得到的参数为:参数1,参数2,输入的内容
    Npc.Say(Player, S);
}

export function InputString1(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    let str: string = Args.Str[0];
    loopMongen(Player.GetMap().GetMapID(), str, Player.MapX, Player.MapY, 5, 1, (list: TActorList) => {

    })
}

export function 查看领取装备(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    let showNameList = []
    let type = Args.Int[0] || 1
    let list = getItemMangae().getAllLv()
    for (let i = 0; i < list.length; i++) {
        showNameList.push({
            name: getItemMangae().getLvNameByIndex(i),
            fucName: `测试.领取装备(${i},${type})`,
        });
    }

    let ItemsString = getShowString(showNameList);

    Npc.Say(Player, ItemsString)
}

export function 领取装备(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    let list: Array<string> = getItemMangae().getItemLvList2(Args.Int[0])
    let type = Args.Int[1] || 1
    for (let i = 0; i < list.length; i++) {
        let name = list[i];
        let aitem: TUserItem = Player.GiveItem(name, false)
        if (aitem) {
            checkItems(aitem, Player, CheckType.All, false, 0, type)
        }
    }
}

export function 领取物品(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    let name: string = Args.Str[0]
    let num: number = Args.Int[1] || 1

    Player.Give(name, num)
}
export function 测试回血(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    Player.V.GM回血 = !Player.V.GM回血
    Main(Npc, Player, Args)
}
export function 伤害提示(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    Player.V.伤害提示 = !Player.V.伤害提示
    Main(Npc, Player, Args)
}
export function 测试鉴定属性列表(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    let showNameList: Array<ShowStringData> = []
    for (let i = 0; i < ItemCustomSet.length; i++) {
        let item = ItemCustomSet[i];
        let key = getCustomAbbKey(i)
        let value = Player.V[key] || 0
        let showItem = {
            name: `${item.text}:${value}(最大值${item.max})`,
            fucName: `@测试.InputInteger1(请输入数值最大值${item.max},${i})`
        } as ShowStringData;
        showNameList.push(showItem)
    }

    let ItemsString = getShowString(showNameList, 0, 0, 200, LineHeight, 2);
    Npc.Say(Player, ItemsString)
}
export function 测试鉴定属性操作(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    let showNameList: Array<ShowStringData> = []
    showNameList.push({
        name: "设置最大值",
        fucName: "测试.鉴定属性最大"
    })

    showNameList.push({
        name: "清空",
        fucName: "测试.清空鉴定属性"
    })

    let ItemsString = getShowString(showNameList, 0, 0, 200, LineHeight, 2);
    Npc.Say(Player, ItemsString)
}

export function InputInteger1(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    let str: number = Args.Int[1];
    let index: number = Args.Int[0];
    let key = getCustomAbbKey(index)
    Player.V[key] = str;
    测试鉴定属性列表(Npc, Player, Args)
    changeCustomAbility(Player)
}

export function 清空鉴定属性(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    for (let i = 0; i < ItemCustomSet.length; i++) {
        let key = getCustomAbbKey(i)
        Player.V[key] = 0;
    }
    changeCustomAbility(Player)
    // 测试鉴定属性列表(Npc, Player, Args)
}
export function 鉴定属性最大(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    for (let i = 0; i < ItemCustomSet.length; i++) {
        let key = getCustomAbbKey(i)
        Player.V[key] = ItemCustomSet[i].max;
    }
    changeCustomAbility(Player)
    // 测试鉴定属性列表(Npc, Player, Args)
}


export function getCustomAbbKey(index: number) {
    return `Custom${ActorCustomKeyList[index]}`
}

export function 等级(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    let lvList = [1, 20, 30, 40, 50, 60, 70, 80, 90, 100, 999]
    let showNameList: Array<ShowStringData> = []
    for (let i = 0; i < lvList.length; i++) {
        showNameList.push({
            name: `${lvList[i]}级`,
            fucName: `测试.改变等级(${lvList[i]})`
        })
    }

    let ItemsString = getShowString(showNameList);
    Npc.Say(Player, ItemsString)
}

export function 改变等级(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    let str: number = Args.Int[0];
    Player.Level = str;
}
export function 扣血(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {

    Player.HP = 1;
}
export function 开始伤害统计(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {

    Player.R.damageLogStart = GameLib.TickCount;
    Player.R.damageLogSum = 0
}
export function 结束伤害统计(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    let sum = Player.R.damageLogSum;
    let time = (GameLib.TickCount - Player.R.damageLogStart) / 1000;

    Player.R.damageLogStart = 0;
    Player.R.damageLogSum = 0

    let str = `在${time}秒内受到${sum}伤害,平均每秒受到${sum / time}伤害`;
    Player.SendMessage(str, 2)
}

export function 打印伤害统计(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    let sum = Player.R.damageLogSum;
    let time = (GameLib.TickCount - Player.R.damageLogStart) / 1000;

    let str = `在${time}秒内受到${sum}伤害,平均每秒受到${sum / time}伤害`;
    Player.SendMessage(str, 2)
}

export function 新技能(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    let list = [
        "逐日剑法", "彻地钉", "灭天火", "流星火雨", "噬血术", "群体施毒", "施毒术"
    ]

    let showNameList = []
    for (let i = 0; i < list.length; i++) {
        let item = list[i]
        let fucName = `@测试.InputInteger2(请输入物品数量最大值999,${item})`
        showNameList.push({
            name: item,
            fucName: fucName,
        })
    }

    let ItemsString = getShowString(showNameList, 0, 0, 120, LineHeight, 3);

    Npc.Say(Player, ItemsString)
}

export function InputInteger2(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    let num: number = Args.Int[1];
    let name: string = Args.Str[0];
    let lv = 0, strlen = 0;
    if (num <= 3) {
        lv = num
    }
    else {
        lv = 3;
        strlen = num - 3;
    }
    Player.DelSkill(name)
    Player.AddSkill(name, lv)
    let magic: TUserMagic = Player.FindSkill(name)
    if (magic) {
        magic.SetStrengthen(strlen)
    }
}

export function 怪物数量(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    let map: TEnvirnoment = GameLib.FindMap('H007')
    Player.SendMessage(`${map.Name}有${map.MonCount}怪物`, 1)
}